package com.yida.system.service.impl;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson2.JSONObject;
import com.yida.common.core.exception.ServiceException;
import com.yida.system.api.domain.SysSms;
import com.yida.system.service.ISysLocalSmsService;

import cn.hutool.core.convert.Convert;
import cn.hutool.http.ContentType;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;

/**
 * @author hmh 参考ESB系统：com.esb.eda.sms.service.impl 下进行临时本地改造。后期需要再行变更
 * @date 2023/11/01
 */
@Service
public class SysLocalSmsServiceImpl implements ISysLocalSmsService {

    private final String CHARSET_UTF = "UTF-8";

    /**
     * GBK
     */
    private final String CHARSET_GBK = "GBK";

    /**
     * 请求地址
     */
    private final String SEND_URL = "";

    /**
     * 发送短信 uri
     */
    private final String SMS_SUFFIX_URI = "/sms/Api/Send.do?";

    /**
     * 回执接口 uri
     */
    private final String RECEIPT_SUFFIX_URI = "/sms/Api/report.do?";

    /* 
     * 根据参数发送短信内容
     */
    @Override
    public void sendSms(SysSms sysSms) {
        String phonenumber = sysSms.getPhonenumber();
        String sms = sysSms.getSms();
        JSONObject json = new JSONObject();
        json.put("UserNumber", phonenumber);
        json.put("MessageContent", sms);
        sendPost(SEND_URL, SMS_SUFFIX_URI, request2Str(json, CHARSET_GBK, 1));
    }

    /**
     * 请求对象转为字符串
     * 
     * @param ssr 请求对象
     * @param charset 字符类型
     * @return
     */
    private String request2Str(JSONObject json, String charset, Integer smsType) {
        StringBuilder sb = new StringBuilder();
        switch (smsType) {
            case 1:
                // sb.append("SpCode=").append(ssr.getSpCode()).append("&").append("LoginName=").append(ssr.getLoginName()).append("&").append("Password=").append(ssr.getPassword()).append("&").append("MessageContent=").append(ssr.getMessageContent()).append("&").append("UserNumber=").append(ssr.getUserNumber()).append("&").append("SerialNumber=").append(ssr.getSerialNumber()).append("&").append("ScheduleTime=").append(ssr.getScheduleTime()).append("&").append("f=").append(ssr.getF());
                // sb.append("SpCode=").append(ssr.getSpCode()).append("&").append("LoginName=").append(ssr.getLoginName()).append("&").append("Password=").append(ssr.getPassword()).append("&").append("MessageContent=").append(ssr.getMessageContent()).append("&").append("UserNumber=").append(ssr.getUserNumber()).append("&").append("SerialNumber=").append(ssr.getSerialNumber()).append("&").append("ScheduleTime=").append(ssr.getScheduleTime()).append("&").append("f=").append(ssr.getF());
                sb.append("SpCode=").append(json.getString("SpCode")).append("&").append("LoginName=").append(json.getString("LoginName")).append("&").append("Password=").append(json.getString("Password")).append("&").append("MessageContent=").append(json.getString("MessageContent")).append("&").append("UserNumber=").append(json.getString("UserNumber")).append("&").append("SerialNumber=").append(json.getString("SerialNumber")).append("&").append("ScheduleTime=")
                    .append(json.getString("ScheduleTime")).append("&").append("f=").append(json.getString("f"));
                break;
            case 2:
                sb.append("SpCode=").append(json.getString("SpCode")).append("&").append("LoginName=").append(json.getString("LoginName")).append("&").append("Password=").append(json.getString("Password"));
                break;
            default:
                break;
        }
        return Convert.convertCharset(sb.toString(), CHARSET_UTF, charset);
    }

    /**
     * 发送Post请求
     * 
     * @param params 请求参数
     * @param ipUrl 请求IP地址
     * @param uri 接口URI
     * @return
     */
    public String sendPost(String ipUrl, String uri, String params) {
        String url = ipUrl + uri + params;
        String result = null;
        try {
            result = HttpUtil.createRequest(Method.POST, url).contentType(ContentType.JSON.getValue()).charset(CHARSET_GBK).execute().body();
        } catch (Exception e) {
            throw new ServiceException("调用短信机异常，请联系管理员");
        }
        return result;
    }

}
